#!/usr/bin/env bash
set -e
set -u
set -o pipefail


get_important_number() {
    echo "NaN"
}


get_some_other_number() {
    echo 9
}


get_module1_important_number() {
    source ${BASH_SOURCE%/*}/module1.sh
    local number_scoped=$(get_important_number)
    echo ${number_scoped}
}


get_module2_important_number() {
    source ${BASH_SOURCE%/*}/module2.sh
    local number_scoped=$(get_important_number)
    echo ${number_scoped}
}


get_module3_important_number() {
    source ${BASH_SOURCE%/*}/subdirectory/module3.sh
    local number_scoped=$(get_important_number)
    echo ${number_scoped}
}


module3_get_some_other_number() {
    source ${BASH_SOURCE%/*}/subdirectory/module3.sh
    local number_scoped=$(get_some_other_number)
    echo ${number_scoped}
}


main() {
    local important_number_unscoped_and_clobbered=$(get_important_number)
    local important_number_module1=$(get_module1_important_number)
    local important_number_module2=$(get_module2_important_number)
    local important_number_module3=$(get_module3_important_number)
    local important_number_module1_again=$(get_module1_important_number)
    local important_number_unscoped_again=$(get_important_number)
    local important_number_module3_again=$(get_module3_important_number)
    local some_other_number=$(get_some_other_number)
    local some_other_number_module3=$(module3_get_some_other_number)


    # NOTE: Run the tests:
    [[ "NaN" == ${important_number_unscoped_and_clobbered} ]] || echo "fail"
    [[ -1 == ${important_number_module1} ]] || echo "fail"
    [[ 42 == ${important_number_module2} ]] || echo "fail"
    [[ 1000000 == ${important_number_module3} ]] || echo "fail"
    [[ -1 == ${important_number_module1_again} ]] || echo "fail"
    [[ "NaN" == ${important_number_unscoped_again} ]] || echo "fail"
    [[ 1000000 == ${important_number_module3_again} ]] || echo "fail"
    [[  9 == ${some_other_number} ]] || echo "fail"
    [[  0 == ${some_other_number_module3} ]] || echo "fail"
}


main $@
